package com.changhai.diabetes.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.arcsoft.common.util.Validator;
import com.arcsoft.webframework.Response;
import com.changhai.diabetes.model.InterPatientGroup;
import com.changhai.diabetes.model.OpertResultMsg;
import com.changhai.diabetes.model.Patient;
import com.changhai.diabetes.model.QueryDTO;
import com.changhai.diabetes.model.ResearchGroup;
import com.changhai.diabetes.model.TableForPatient;
import com.changhai.diabetes.model.page.Page;
import com.changhai.diabetes.service.CommonService;
import com.changhai.diabetes.service.GroupService;
import com.changhai.diabetes.service.InterPatientGroupService;
import com.changhai.diabetes.service.PatientService;

/**
 * 患者信息管理
 * 
 * @author Andy
 * 
 */
public class PatientAction extends BasePaginationAction {

	private static final long serialVersionUID = 4392936553263004630L;
	private OpertResultMsg opertMsg;
	private Long patientid;
	private String patientname;
	private String crenum;
	private String orderCol;// 排序字段
	private Patient patient;
	private InterPatientGroup interPG;
	private PatientService patientService;
	private GroupService groupService;
	private InterPatientGroupService interPatientGroupService;
	private List<ResearchGroup> groupList;
	private Integer flag = 0;
	private Long researchgroupid;
	 
	
	public String execute() throws Exception {
		return super.execute();
	}

	public String patient() {
		return this.query();
	}

	@Override
	protected Page searchQuery(QueryDTO queryDTO) {
		Map map = new HashMap();
		this.setSortInfo(this.getOrderCol(), this.getPage(), map);// 设置排序信息
		queryDTO.setConditions(map);
		if (patientid != null && !Validator.isBlank(patientid.toString())) {
			queryDTO.getConditions().put("patientid", patientid.toString());
		}
		if (!Validator.isBlank(patientname)) {
			queryDTO.getConditions().put("patientname", "%" + patientname.trim() + "%");
		}
		if (!Validator.isBlank(crenum)) {
			queryDTO.getConditions().put("crenum", crenum);
		}
		if(researchgroupid!=null && researchgroupid>=0){
			queryDTO.getConditions().put("researchgroupid", researchgroupid);
		}
		return patientService.searchQuery(queryDTO);
	}

	public String insertNewPatient() {
		groupList = groupService.getGroupList();
		return "input";
	}

	public String editPatient() {
		if (patientid != null && patientid > 0) {
			try {
				patient = patientService.searchPatientById(patientid);
				groupList = groupService.getGroupList();
				interPG = interPatientGroupService.getGroupByPatientId(patientid);
				tbList = this.getTableForEnter(patientid, flag);
			} catch (Exception e) {
				e.printStackTrace();
				return "query";
			}
			return "input";
		} else {
			return "query";
		}
	}

	public String saveEditPatient() {
		if (patientid == null || patientid <= 0 || patient == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				patientService.saveEditPatient(patient);
				interPatientGroupService.savePatientGroup(interPG);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("update", "基本信息表", patientid);
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.editPatient();
	}

	public String saveNewPatient() {
		if (patient == null) {
			opertMsg = new OpertResultMsg(Long.valueOf(1), "参数错误!");
		} else {
			try {
				patientid = patientService.saveNewPatient(patient);
				interPG.setPatientid(patientid);
				interPatientGroupService.saveNewPatientGroup(interPG);
				opertMsg = new OpertResultMsg(Long.valueOf(0), "保存成功!");
				commonService.writeLogToFile("insert", "基本信息表", patientid);
				return this.editPatient();
			} catch (Exception e) {
				opertMsg = new OpertResultMsg(Long.valueOf(1), "保存失败!");
			}
		}
		return this.insertNewPatient();
	}

	public Response delPatient(){
		Response res = new Response(); 
		if(patientid==null || patientid<=0){
			res.setErrorMessage("error");
		}else{
			try {
				patientService.delPatient(patientid);
				res.setSuccessMessage("success");
				commonService.writeLogToFile("delete", "患者信息表", patientid);
			} catch (Exception e) {		 
				e.printStackTrace();
				res.setErrorMessage("error");
			}
		}
		return res;
	}
	
	public OpertResultMsg getOpertMsg() {
		return opertMsg;
	}

	public void setOpertMsg(OpertResultMsg opertMsg) {
		this.opertMsg = opertMsg;
	}

	public Long getPatientid() {
		return patientid;
	}

	public void setPatientid(Long patientid) {
		this.patientid = patientid;
	}

	public String getPatientname() {
		return patientname;
	}

	public void setPatientname(String patientname) {
		this.patientname = patientname;
	}

	public String getCrenum() {
		return crenum;
	}

	public void setCrenum(String crenum) {
		this.crenum = crenum;
	}

	public String getOrderCol() {
		return orderCol;
	}

	public void setOrderCol(String orderCol) {
		this.orderCol = orderCol;
	}

	public Patient getPatient() {
		return patient;
	}

	public void setPatient(Patient patient) {
		this.patient = patient;
	}

	public PatientService getPatientService() {
		return patientService;
	}

	public void setPatientService(PatientService patientService) {
		this.patientService = patientService;
	}

	public GroupService getGroupService() {
		return groupService;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	public InterPatientGroup getInterPG() {
		return interPG;
	}

	public void setInterPG(InterPatientGroup interPG) {
		this.interPG = interPG;
	}

	public List<ResearchGroup> getGroupList() {
		return groupList;
	}

	public InterPatientGroupService getInterPatientGroupService() {
		return interPatientGroupService;
	}

	public List<TableForPatient> getTbList() {
		return tbList;
	}

	public void setTbList(List<TableForPatient> tbList) {
		this.tbList = tbList;
	}

	public void setInterPatientGroupService(InterPatientGroupService interPatientGroupService) {
		this.interPatientGroupService = interPatientGroupService;
	}

	public void setGroupList(List<ResearchGroup> groupList) {
		this.groupList = groupList;
	}

	public Integer getFlag() {
		return flag;
	}

	public void setFlag(Integer flag) {
		this.flag = flag;
	}

	public Long getResearchgroupid() {
		return researchgroupid;
	}

	public void setResearchgroupid(Long researchgroupid) {
		this.researchgroupid = researchgroupid;
	}
}
